package com.travel.server.manager.impl.dal;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.CustomerTagInfoDao;
import com.travel.server.manager.model.CustomerTagInfo;
import com.travel.server.manager.model.CustomerTagInfoExample;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/6 19:17
 * @Desc:
 */
@Component
public class CustomerTagDal {

    @Autowired
    private CustomerTagInfoDao customerTagInfoDao;

    public List<CustomerTagInfo> selectByExample(CustomerTagParam param){
        CustomerTagInfoExample example = makeExample(param);
        return customerTagInfoDao.selectByExample(example);
    }

    public void insertOrUpdate(CustomerTagInfo customerTagInfo){
        if(customerTagInfo.getId() == null){
            customerTagInfoDao.insertSelective(customerTagInfo);
        }else{
            customerTagInfoDao.updateByPrimaryKey(customerTagInfo);
        }
    }

    private CustomerTagInfoExample makeExample(CustomerTagParam param){
        CustomerTagInfoExample example = new CustomerTagInfoExample();
        CustomerTagInfoExample.Criteria criteria = example.or();
        if(!CollectionUtil.isEmpty(param.getTagIdList())){
            criteria.andTagIdIn(param.getTagIdList());
        }
        if(param.getCustomerId() != null){
            criteria.andCustomerIdEqualTo(param.getCustomerId());
        }
        if(param.getState() != null){
            criteria.andStateEqualTo(param.getState());
        }
        return example;
    }

    @Data
    public static class CustomerTagParam{
        private List<Integer> tagIdList;

        private Integer customerId;

        private Integer state = 1;
    }
}
